﻿using _01_Tower4._0.CharacterBase;
using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.Interface;
using _01_Tower4._0.Items;
using _01_Tower4._0.Items.Base;
using _01_Tower4._0.Scene;
using _01_Tower4._0.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01_Tower4._0.GameFrame.System
{
    internal class InventorySystem
    {
        List<Item> items;
        int x, y, width, height;
        int selection, maxShow;
        int startIndex, endIndex;
        public static bool IsOpen;
        public static bool IsUse;
        Character owner;
        int clearIndex;

        public InventorySystem(int x, int y, int width, int height,Character owner)
        {
            items = new List<Item>();
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.owner = owner;
            selection = 0;
            maxShow = height - 2;
            startIndex = 0;
            endIndex = 0;

            EventSystem.Instance.AddEvent("生命水晶", () =>
            {
                Add(new RedPotion());
            });
            EventSystem.Instance.AddEvent("史莱姆凝胶", () =>
            {
                Add(new SlimeGel());
            });
            EventSystem.Instance.AddEvent("哥布林长矛", () =>
            {
                Add(new GoblinFall());
            });
            EventSystem.Instance.AddEvent("蓝药", () =>
            {
                Add(new BluePotion());
            });
            #region Key
            EventSystem.Instance.AddEvent("钥匙1.0", () =>
            {
                Add(new Key1_0());
            });
            EventSystem.Instance.AddEvent("钥匙2.0", () =>
            {
                Add(new Key2_0());
            });
            EventSystem.Instance.AddEvent("钥匙2.1", () =>
            {
                Add(new Key2_1());
            });
            #endregion

        }

        public void Init()
        {
            IsOpen = false;
        }

        public void Add(Item item)
        {
            items.Add(item);
            MoveShowIndex(true);
        }

        public void Remove(Item item)
        {
            items.Remove(item);
            MoveShowIndex(false);
        }

        public void Input()
        {
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                UpRoll();
                Clear();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                DownRoll();
                Clear();
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Enter))
            {
                if (startIndex + selection < items.Count)
                {
                    Use(items[startIndex + selection]);
                }
                if (SceneManager.lastSceneName == "SceneCombat")
                {
                    IsOpen = false;
                    IsUse = true;
                    SceneManager.sceneName = "SceneCombat";
                }
            }
            if (InputSystem.Instance.IsKeyPressed(ConsoleKey.Escape))
            {
                if (SceneManager.lastSceneName == "SceneCombat")
                {
                    IsOpen = false;
                    CombatSystem.BagIsOpen = true;
                    SceneManager.sceneName = "SceneCombat";
                }
            }
        }

        public void Render()
        {
            clearIndex = 0;
            if (startIndex + selection < items.Count)
            {
                Description(items[startIndex + selection]);
            }
            Utility.DrawFrame(x, y, width, height, ConsoleColor.White);

            if (items.Count > 0)
            {
                for (int i = startIndex, j = 0; i <= endIndex; i++, j++)
                {
                    Utility.MyPrint(items[i].Name, x + 2, y + 1 + j);
                }
            }
            Utility.MyPrint("背包", x + (width / 2) - 2, y - 1);
            Utility.MyPrint(">", x + 1, y + 1 + selection);
            Utility.MyPrint("<", x + width - 2, y + 1 + selection);
        }

        public void Clear()
        {
            for (int j = 0; j < height + 1; j++)
            {
                for (int i = 0; i < width+16; i++)
                {
                    Utility.MyPrint(" ", x + i, y - 1 + j);
                }
            }
            Utility.MyPrint("                                      ", x + 2, y + height + 1);
            Utility.MyPrint("                                      ", x + 2, y + height + 2);
        }

        public void Update()
        {
            if (!IsOpen && clearIndex < 1)
            {
                Clear();
                clearIndex++;
            }
        }
        public void Use(Item item, IAttack? target = null)
        {
            Type? type = item.GetType().BaseType;
            if (type == typeof(Consumable))
            {
                ((Consumable)item).Use(owner);
                Remove(item);
            }
            else if (type == typeof(KeyBase))
            {
                if (((KeyBase)item).Use())
                {
                    Remove(item);
                }
            }
            
        }

        public void Description(Item item)
        {
            Utility.MyPrint($"<{item.Name}>", x + 2, y + height + 1);
            Utility.MyPrint(item.Description, x + 2, y + height + 2);
        }

        void UpRoll()
        {
            selection--;
            if (selection < 0)
            {
                selection = 0;
                // 将道具列表往下移一行
                if (items.Count > maxShow)
                {
                    MoveShowIndex(true);
                }
            }
        }

        void DownRoll()
        {
            if (selection < items.Count - 1)
                selection++;

            if (selection > maxShow - 1)
            {
                selection = maxShow - 1;
                // 将道具列表往上移一行
                if (items.Count > maxShow)
                {
                    MoveShowIndex(false);
                }
            }
        }

        void MoveShowIndex(bool up)
        {
            int offset = 0;
            if (items.Count > maxShow)
            {
                offset = maxShow - 1;
            }
            else
            {
                offset = items.Count - 1;
            }

            if (up)
            {
                startIndex--;
                if (startIndex < 0)
                {
                    startIndex = 0;
                }
                endIndex = startIndex + offset;
            }
            else
            {
                endIndex++;
                if (endIndex > items.Count - 1)
                {
                    endIndex = items.Count - 1;
                }
                startIndex = endIndex - offset;
            }
        }
    }
}
